<?php

namespace App\Models;

use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;

class Topic extends Model
{

	use HasDateTimeFormatter;

	public $select = ['id', 'title', 'user_id', 'category_id', 'reply_count', 'view_count', 'order', 'created_at'];

	public function user()
    {
        return $this->hasOne(User::class, 'id', 'user_id')->select(['id', 'name', 'head_img', 'email']);
    }

    public function category()
    {
        return $this->hasOne(TopicCategory::class, 'id', 'category_id');
    }

    public function reply()
    {
        return $this->hasMany(Reply::class, 'topic_id', 'id');
    }

    public function getRow($id, $with = [], $select = ['*'])
    {
        return $this
            ->newQuery()
            ->when($with, function ($query, $with) {
                return $query->with($with);
            })
            ->when($select, function ($query, $select) {
                return $query->select($select);
            })
            ->find($id);
    }

    public function getList($where = [], $with = [], $select = ['*'], $page = 1, $perPage = 10, $order = ['created_at', 'desc'])
    {
        return $this
            ->newQuery()
            ->when($where, function ($query, $where) {
                return $query->where($where);
            })
            ->when($with, function ($query, $with) {
                return $query->with($with);
            })
            ->when($select, function ($query, $select) {
                return $query->select($select);
            })
            ->forPage($page, $perPage)
            ->when($order, function ($query, $order) {
                return $query->orderBy($order[0], $order[1]);
            })
            ->orderBy('order')
            ->get()
            ->toArray();
    }

    // 返回当前页数据列和总数据量
    public function getPaginate($where = [], $with= [], $select = ['*'], $page = 1, $perPage = 10, $order = [])
    {

        $data = $this->newQuery()
            ->when($where, function ($query, $where) {
                return $query->where($where);
            })
            ->when($with, function ($query, $with) {
                return $query->with($with);
            })
            ->when($order, function ($query, $order) {
                return $query->orderBy($order[0], $order[1]);
            })
            ->orderBy('order')
            ->paginate($perPage, $select, 'page', $page);

        return ['list' => $data->toArray()['data'], 'total' => $data->total()];
    }

    // 排序规则
    public function getOrder($order)
    {
        switch ($order) {
            case 'reply': // 按评论
                $data = ['reply_count', 'desc'];
                break;
            case 'hot': // 按浏览
                $data = ['view_count', 'desc'];
                break;
            default: // 按最新
                $data = ['created_at', 'desc'];
                break;
        }

        return $data;
    }

    // 本周热议
    public function weekHot()
    {
        return $this->getList([], [], ['id', 'title', 'reply_count'], 1, 10, ['reply_count', 'desc']);
    }

    public function addData($data)
    {
        return $this->newQuery()->insertGetId($data);
    }

}
